package de.tabuma.shopping.controller;

import de.tabuma.lib.*;

import de.tabuma.shopping.*;
import de.tabuma.shopping.entity.*;
import de.tabuma.shopping.logik.*;

import java.io.*;
import java.lang.reflect.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.util.logging.*;

import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class licenseassignmenttable extends tabumacontroller {

    private static final Logger log = Logger.getLogger(licenseassignmenttable.class.getName());

    private List<LicenseType> licenseTypeList;
    private Class assignmentClass;
    private Field assignmentKey;
    private Field assignmentShow;
    private List assignmentList;
    private String assignmentObjectname;
    LicensePackageFacade licensePackageFacade;
    Hashtable<Integer,String> methodList;
    public licenseassignmenttable(String servlet, Hashtable parameter, LicensePackageFacade licensePackageFacade, List<LicenseType> licenseTypeList, Class assignmentClass, Field assignmentKey, Field assignmentShow, List assignmentList, String assignmentObjectname, Hashtable<Integer,String> methodList) {
        super(servlet, parameter);
        this.licenseTypeList = licenseTypeList;
        this.assignmentClass = assignmentClass;
        this.assignmentKey = assignmentKey;
        this.assignmentList = assignmentList;
        this.assignmentShow = assignmentShow;
        this.assignmentObjectname = assignmentObjectname;
        this.licensePackageFacade = licensePackageFacade;
        this.methodList = methodList;
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if(request.getParameter(constants.ORDER_LICENSE_ASSIGNMENT_ID)!=null) {
            String licenseAssignmentId = request.getParameter(constants.ORDER_LICENSE_ASSIGNMENT_ID);
            int licenseAssignmentIdInt = Integer.valueOf(licenseAssignmentId).intValue();
            if(request.getParameter(constants.ORDER_LICENSE_ASSIGNMENT_REMOVE)!=null) {
                LicenseAssignment la = LicenseAssignmentFacade.get(licenseAssignmentIdInt);
                if(la.getLicenseUsed()==0) {
                    LicenseAssignmentFacade.remove(la, user.getEmail());
                }
                else {
                    la.setLicenseCount(la.getLicenseUsed());
                    LicenseAssignmentFacade.update(la, user.getEmail());
                }
            }
            else {
                request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_ID, licenseAssignmentId);
                request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_METHOD_LIST, this.methodList);
                List<LicenseUse> luList = LicenseUseFacade.getList(licenseAssignmentIdInt);
                request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_USED_LICENSES_LIST, luList);
            }
        }

        if(request.getParameter(constants.ORDER_LICENSE_ASSIGNMENT_ADD)!=null) {
            String orderLicenseTypeNr = request.getParameter(constants.ORDER_LICENSE_TYPE_NR);
            int orderLicenseTypeNrInt = Integer.valueOf(orderLicenseTypeNr).intValue();
            String ORDER_LICENSE_ASSIGNMENT_ADD_ID = request.getParameter(constants.ORDER_LICENSE_ASSIGNMENT_ADD_ID);
            String licenseAssignmentId = request.getParameter(constants.ORDER_LICENSE_ASSIGNMENT_ID);
            int licenseAssignmentIdInt = Integer.valueOf(licenseAssignmentId).intValue();
            String licenseAssignmentCount = request.getParameter(constants.ORDER_LICENSE_ASSIGNMENT_COUNT);
            int licenseAssignmentCountInt = Integer.valueOf(licenseAssignmentCount).intValue();
            List<OrderLicensePackageRelation> payedOlprList = getPayedOrderLicensePackageRelation();
            for(int j=0;j<payedOlprList.size();j++) {
                OrderLicensePackageRelation olpr = payedOlprList.get(j);
                LicensePackage lp = this.licensePackageFacade.getLicensePackageWithID(olpr.getLicensePackageId());
                if(lp!=null) {
                    for(int k=0;k<lp.getList().length;k++) {
                        License license = lp.getList()[k];

                        if(license.getLicenseTyp()==orderLicenseTypeNrInt) {
                            String nr = olpr.getId()+"_"+license.getId()+"_"+k;
                            if(nr.equals(ORDER_LICENSE_ASSIGNMENT_ADD_ID)) {
                                LicenseAssignment la = new LicenseAssignment();
                                la.setAssignmentId(licenseAssignmentIdInt);
                                la.setLicenseCount(licenseAssignmentCountInt);
                                la.setOrderLicensePackageRelationId(olpr.getId().intValue());
                                la.setLicenseType(orderLicenseTypeNrInt);
                                la.setNrInOrder(k);
                                LicenseAssignmentFacade.create(la, user.getEmail());
                            }
                        }
                    }
                }
            }
        }

        Hashtable<Integer,Integer> assignmentLicense = new Hashtable<Integer,Integer>();
        Hashtable<Integer,Integer> usedLicense = new Hashtable<Integer,Integer>();
        request.setAttribute(constants.ORDER_LICENSE_TYPE_LIST, this.licenseTypeList);
        for(int i=0;i<this.licenseTypeList.size();i++) {
            LicenseType licenseType = this.licenseTypeList.get(i);
            int count = LicenseAssignmentFacade.getCountAssignedLicenses(licenseType.getId());
            assignmentLicense.put(new Integer(licenseType.getId()), count);
            int used = LicenseAssignmentFacade.getCountUsedLicenses(licenseType.getId());
            usedLicense.put(new Integer(licenseType.getId()), used);
        }
        request.setAttribute(constants.ORDER_LICENSE_TYPE_LIST, this.licenseTypeList);
        request.setAttribute(constants.ORDER_LICENSE_ASSIGNED_LICENSE, assignmentLicense);
        request.setAttribute(constants.ORDER_LICENSE_USED_LICENSE, usedLicense);
        request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_CLASS, this.assignmentClass);
        request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_FIELD_KEY, this.assignmentKey);
        request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_FIELD_SHOW, this.assignmentShow);
        request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_OBJECT_LIST, this.assignmentList);
        List<Order> oList = OrderFacade.getOrderList(user.getEmail());
        List<OrderLicensePackageRelation> olprList = new ArrayList();
        for(int i=0;i<oList.size();i++) {
            olprList.addAll(OrderLicensePackageRelationFacade.getList(oList.get(i).getId().intValue()));
        }

        Hashtable<String,Integer> freeLicenses = new Hashtable<String,Integer>();
        for(int i=0;i<olprList.size();i++) {
            OrderLicensePackageRelation olpr = olprList.get(i);
            LicensePackage lp = this.licensePackageFacade.getLicensePackageWithID(olpr.getLicensePackageId());
            for(int j=0;j<lp.getList().length;j++) {
                String key = olpr.getId().intValue()+"_"+j;
                Integer value = LicenseAssignmentFacade.getUnusedLicenseForOrderedLicense(olpr.getId().intValue(), j, licensePackageFacade);
                freeLicenses.put(key,value);
            }
        }
        request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_UNUSED_LICENSES_HASH, freeLicenses);

        request.setAttribute(constants.ORDER_LICENSE_RELATION_LIST, olprList);
        List<LicensePackage> lpList = licensePackageFacade.getAllLicensePackageList();



        request.setAttribute(constants.ORDER_ALL_LICENSEPACKAGE_LIST, lpList);
        request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_OBJECTNAME, this.assignmentObjectname);

        if(request.getParameter(constants.ORDER_LICENSE_TYPE_NR)!=null) {
            String orderLicenseTypeNr = request.getParameter(constants.ORDER_LICENSE_TYPE_NR);
            int orderLicenseTypeNrInt = Integer.valueOf(orderLicenseTypeNr).intValue();
            request.setAttribute(constants.ORDER_LICENSE_TYPE_NR, orderLicenseTypeNr);
            List<LicenseAssignment> list = LicenseAssignmentFacade.getList(orderLicenseTypeNrInt);
            request.setAttribute(constants.ORDER_LICENSE_ASSIGNMENT_LIST, list);
        }

    }

    public static List<OrderLicensePackageRelation> getPayedOrderLicensePackageRelation() {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        List<Order> oList = OrderFacade.getOrderList(user.getEmail());
        List<OrderLicensePackageRelation> olprList = new ArrayList<OrderLicensePackageRelation>();
        for(int i=0;i<oList.size();i++) {
            Order order = oList.get(i);
            if(order.getPayed()) {
                olprList.addAll(OrderLicensePackageRelationFacade.getList(order.getId().intValue()));
            }
        }
        return olprList;
    }
}